﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using My;

public partial class WebForm_EmpData_tabPrimary : System.Web.UI.UserControl
{
    protected void Page_Init(object sender, EventArgs e)
    {
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        fv.Saving += new EventHandler<MyFormViewSavingEventArgs>(fv_Saving);
    }

    void fv_Saving(object sender, MyFormViewSavingEventArgs e)
    {
        var db = new DB();

        var paras = new List<DBParameter>();
        var query = string.Empty;

        if (fv.CurrentMode == FormViewMode.Insert)
        {
            query = string.Format(@"INSERT INTO [dbo].[EmpData]
           ([EmpID]
           ,[NTAccount]
           ,{0}
           ,[ComeDate]
           ,[OrgID]
           ,[Title]
           ,EmpLevel
           ,[Email]
           ,[Status]
           ,[CreateBy]
           ,[CreateDate])
     VALUES
           (@EmpID, @NTAccount, @EmpName, @ComeDate, @OrgID
           , @Title, @EmpLevel, @Email, @Status, @CreateBy, getdate()) ", LocManager.Current.GetTextCol("EmpData", "EmpName"));

            paras.Add(new DBParameter("CreateBy", EmpManager.Current.EmpID));
            paras.Add(new DBParameter("Status", "Y"));
        }
        else // Update
        {
            query = string.Format(@"UPDATE [dbo].[EmpData]
   SET [NTAccount] = @NTAccount
      ,{0} = @EmpName
      ,OrgID = @OrgID
      ,Title = @Title
      ,EmpLevel = @EmpLevel
      ,Email = @Email
      ,UpdateBy = @UpdateBy
      ,UpdateDate = getdate()
 WHERE EmpID = @EmpID
", LocManager.Current.GetTextCol("EmpData", "EmpName"));

            paras.Add(new DBParameter("UpdateBy", EmpManager.Current.EmpID));
        }

        paras.Add(new DBParameter("EmpID", (this.FindControl("txtEmpID") as ITextControl).Text));
        paras.Add(new DBParameter("EmpName", (this.FindControl("txtEmpName") as ITextControl).Text));
        paras.Add(new DBParameter("NTAccount", (this.FindControl("txtNTAccount") as ITextControl).Text));
        paras.Add(new DBParameter("Password", string.Empty));
        paras.Add(new DBParameter("ComeDate", DateTime.Now));
        paras.Add(new DBParameter("OrgID", (this.FindControl("ddlOrgID") as ITextControl).Text));
        paras.Add(new DBParameter("Title", (this.FindControl("ddlTitle") as ITextControl).Text));
        paras.Add(new DBParameter("EmpLevel", (this.FindControl("ddlEmpLevel") as ITextControl).Text));
        paras.Add(new DBParameter("Email", string.Empty));

        db.ExecuteNonQuery(query, paras);
    }

    protected MyFormView fv
    {
        get
        {
            Control container = NamingContainer;

            while (container != Page)
            {
                if (container is FormView)
                {
                    return container as MyFormView;
                }

                container = container.NamingContainer;
            }

            return null;
        }
    }
}